package com.ip.user.api;

import com.ip.user.models.common.annotation.RequiresPermission;
import com.ip.user.models.service.SysUserService;
import com.ip.user.models.vo.SysLoginLogVO;
import com.ip.common.constant.PageResult;
import com.ip.common.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/api/sys-login-logs")
public class SysLoginLogController {
    
    @Autowired
    private SysUserService sysUserService;
    
    /**
     * 获取所有登录日志（支持分页）
     */
    @GetMapping("/list")
    @RequiresPermission("sys:loginlog:list")
    public PageResult<List<SysLoginLogVO>> getAllLoginLogs(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            return sysUserService.getAllLoginLogs(pageNo, pageSize);
        } catch (Exception e) {
            PageResult<List<SysLoginLogVO>> result = new PageResult<>();
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(e.getMessage());
            result.setData(null);
            return result;
        }
    }
    
    /**
     * 根据多个条件组合查询登录日志
     */
    @GetMapping("/conditions")
    @RequiresPermission("sys:loginlog:list")
    public PageResult<List<SysLoginLogVO>> getLoginLogsByConditions(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer loginStatus,
            @RequestParam(required = false) String userName,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            
            // 解析时间参数
            LocalDateTime start = null;
            LocalDateTime end = null;
            
            if (startTime != null && !startTime.isEmpty()) {
                start = LocalDateTime.parse(startTime, formatter);
            }
            
            if (endTime != null && !endTime.isEmpty()) {
                end = LocalDateTime.parse(endTime, formatter);
            }
            
            // 如果提供了开始时间但没有提供结束时间，则将结束时间设为当前时间
            if (start != null && end == null) {
                end = LocalDateTime.now();
            }
            
            // 如果提供了结束时间但没有提供开始时间，则将开始时间设为30天前
            if (end != null && start == null) {
                start = end.minusDays(30);
            }
            
            // 查询符合条件的日志
            return sysUserService.getLoginLogsByConditions(start, end, loginStatus, userName, pageNo, pageSize);
        } catch (Exception e) {
            PageResult<List<SysLoginLogVO>> result = new PageResult<>();
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg("查询失败: " + e.getMessage());
            result.setData(null);
            return result;
        }
    }
}